﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OctopusWebInterface
{
    public partial class GetRdp : System.Web.UI.Page
    {
        string rdpTemplate = 
                    "screen mode id:i:2" + "\r\n" +
                    "session bpp:i:16" + "\r\n" +
                    "winposstr:s:2,3,451,183,1108,683" + "\r\n" +
                    "full address:s:{0}:3389" + "\r\n" +
                    "compression:i:1" + "\r\n" +
                    "keyboardhook:i:2" + "\r\n" +
                    "displayconnectionbar:i:1" + "\r\n" +
                    "autoreconnection enabled:i:1" + "\r\n" +
                    "alternate shell:s:" + "\r\n" +
                    "shell working directory:s:" + "\r\n" +
                    "disable wallpaper:i:1" + "\r\n" +
                    "disable full window drag:i:1" + "\r\n" +
                    "disable menu anims:i:1" + "\r\n" +
                    "disable themes:i:0" + "\r\n" +
                    "disable cursor setting:i:0" + "\r\n" +
                    "bitmapcachepersistenable:i:1" + "\r\n" +
                    "allow desktop composition:i:0" + "\r\n" +
                    "allow font smoothing:i:0" + "\r\n" +
                    "redirectclipboard:i:1" + "\r\n" +
                    "redirectposdevices:i:0" + "\r\n" +
                    "authentication level:i:0" + "\r\n" +
                    "prompt for credentials:i:0" + "\r\n" +
                    "negotiate security layer:i:1" + "\r\n" +
                    "remoteapplicationmode:i:0" + "\r\n" +
                    "gatewayhostname:s:" + "\r\n" +
                    "gatewayusagemethod:i:4" + "\r\n" +
                    "gatewaycredentialssource:i:4" + "\r\n" +
                    "gatewayprofileusagemethod:i:0" + "\r\n" +
                    "promptcredentialonce:i:1" + "\r\n" +
                    "drivestoredirect:s:" + "\r\n" +
                    "username:s:{1}\\Administrator";

        protected void Page_Load(object sender, EventArgs e)
        {
            string guidStr = this.Request.QueryString.Get("guid");
            if (guidStr == null || guidStr.Trim() == "")
            {
                this.LiteralVmName.Text = "Invalid GUID";
                return;
            }

            Guid guid = new System.Guid(guidStr);
            var vm = Octopus.OctopusLib.Functions.getVmByGuid(guid);
            this.LiteralVmName.Text = vm.HostName;

            string vmState = Octopus.OctopusLib.Functions.getVmStateByGuid(guid);
            if (vmState != "Running")
            {
                this.LiteralIpAddress.Text = "This virtual machine is not running!";
                return;
            }

            try
            {
                string rdpIp = Octopus.OctopusLib.Functions.getVmRDPAddressByGuid(guid);
                string hostname = vm.HostName;
                string instance = String.Format(rdpTemplate, rdpIp, hostname);

                this.Response.ContentType = "text/plain";
                this.Response.AppendHeader("content-disposition", String.Format("attachment; filename={0}.rdp", hostname));
                this.Response.Output.Write(instance);
                this.Response.Close();
            }
            catch
            {
                string ip = Octopus.OctopusLib.Functions.getVmIPv4AddressByGuid(guid);
                this.LiteralIpAddress.Text = ip;
            }
        }
    }
}